//
//  CustomDraverView.m
//  Meroad
//
//  Created by 沈煜航 on 2019/2/2.
//  Copyright © 2019 郭孝通. All rights reserved.
//

#import "CustomDraverView.h"
#import "AppDelegate.h"


//#define kDraverHeight   [[UIScreen mainScreen] bounds].size.height - (IS_IPHONE_XS ? myTabbarHeight + 35 : myTabbarHeight )
#define kDraverHeight   [[UIScreen mainScreen] bounds].size.height
#define kDraverWidth    [[UIScreen mainScreen] bounds].size.width / 2.3

@interface CustomDraverView(){
    NSMutableArray* _imageArray;    //cell中左侧的图标数组
    NSMutableArray* _textArray;     //cell中的文字数组
    
    UIView *_curtainView;        //用于遮挡屏幕
    
    UITapGestureRecognizer* _tap;
}

@end

@implementation CustomDraverView

-(instancetype)init{
    self = [super initWithFrame:CGRectMake(-kDraverWidth, 0, kDraverWidth, kDraverHeight)];
    if(self){
        self.DraverTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        
        self.DraverTable.delegate = self;
        self.DraverTable.dataSource = self;
        
        AppDelegate *_rootDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        self.userModel = _rootDelegate.userModel;
        
        _curtainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width , kDraverHeight)];
        _curtainView.alpha = 0.6;
        _curtainView.backgroundColor = [UIColor whiteColor];
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
        [_curtainView addGestureRecognizer:_tap];
        
        _imageArray = [NSMutableArray array];
        _textArray = [NSMutableArray array];
        
        
        NSString* str = [NSString stringWithFormat:@"当前位置"];
        [_textArray addObject:str];
        UIImage* image = [UIImage imageNamed:@"当前位置-深灰"];
        [_imageArray addObject:image];
        
        str = [NSString stringWithFormat:@"检查更新"];
        [_textArray addObject:str];
        image = [UIImage imageNamed:@"检查更新-深灰"];
        [_imageArray addObject:image];
        
        str = [NSString stringWithFormat:@"用户反馈"];
        [_textArray addObject:str];
        image = [UIImage imageNamed:@"用户反馈-深灰"];
        [_imageArray addObject:image];
        
        str = [NSString stringWithFormat:@"产品评价"];
        [_textArray addObject:str];
        image = [UIImage imageNamed:@"产品评价-深灰"];
        [_imageArray addObject:image];
        
        str = [NSString stringWithFormat:@"通用设置"];
        [_textArray addObject:str];
        image = [UIImage imageNamed:@"通用设置-深灰"];
        [_imageArray addObject:image];
        
        [self addSubview:self.DraverTable];
    }
    return self;
}

+(instancetype)draverView{
    return [[self alloc] init];
}


-(void)show{
    if(self.frame.origin.x != 0){
        [self.DraverTable reloadData];
        [UIView animateWithDuration:0.5 animations:^{
            [self setFrame:CGRectMake(0, 0, kDraverWidth, kDraverHeight)];
        }];
        [self.superview insertSubview:_curtainView atIndex:self.superview.subviews.count - 2];
    }
}

-(void)hide{
    if(self.frame.origin.x == 0){
        [UIView animateWithDuration:0.5 animations:^{
            [self setFrame:CGRectMake(-kDraverWidth, 0, kDraverWidth, kDraverHeight)];
        }];
        [_curtainView removeFromSuperview];
    }
}


#pragma 代理

//返回多少组数据
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
    return 1;
}


//每一组返回多少行
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

//每一行显示数据
    //UITableViewCell：每一行的格子
    //indexPath：代表唯一的一行
    //indexPath.section：获取对应的组号
    //indexPath.row：获取行号
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //第几个cell
    NSInteger num = indexPath.row;
    //建立标志符字符串
    static NSString* ID = @"Cell";
    //让cell从缓存池中出来
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle   reuseIdentifier:ID];
        cell.tag = num;
        switch (num) {
            case 0:
            {
                UIImage *image = self.userModel.userHeaderImage;
                CGSize imageSize = CGSizeMake((80-5*2), (80-5*2));
                UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
                CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height);
                [image drawInRect:imageRect];
                cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsGetImageFromCurrentImageContext();
                cell.imageView.layer.cornerRadius = cell.imageView.bounds.size.height / 2;
                cell.imageView.layer.masksToBounds = true;
                cell.imageView.layer.borderWidth = 1;
                cell.imageView.layer.borderColor = [[UIColor whiteColor] CGColor];
                
                [cell.textLabel setText: self.userModel.userName];
                
                
                break;
            }
                
            
            default:
                [cell.imageView setImage:[_imageArray objectAtIndex:num-1]];
                [cell.imageView setFrame:CGRectMake(5, 5, (40-5*2), (40-5*2))];
                [cell.textLabel setText:[_textArray objectAtIndex:num-1]];
                break;
        }
    }
    else{
        if(num == 0){
            if(cell.imageView.image != self.userModel.userHeaderImage){
                UIImage *image = self.userModel.userHeaderImage;
                CGSize imageSize = CGSizeMake((80-5*2), (80-5*2));
                UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
                CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height);
                [image drawInRect:imageRect];
                cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsGetImageFromCurrentImageContext();
                cell.imageView.layer.cornerRadius = cell.imageView.bounds.size.height / 2;
                cell.imageView.layer.masksToBounds = true;
                cell.imageView.layer.borderWidth = 1;
                cell.imageView.layer.borderColor = [[UIColor whiteColor] CGColor];
            }
            
            if(cell.textLabel.text != self.userModel.userName){
                [cell.textLabel setText: self.userModel.userName];
            }
        }
    }
    
    return cell;
}

//返回头部文字
-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
    return NULL;
}

//返回尾部文字
-(NSString*)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section{
    return @"MeRoad";
}

//设置某一行cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 0) {
        return 80;
    }
    return 40;
}

//当选中某一行cell时调用该方法
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
    [self.delegate draverViewDidSelectRowNumber:indexPath.row];
}

@end
